home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 July / Macworld (1999-07).dmg / Shareware World / Info / For Developers / Mops 3.4.sea / Mops source / More classes / Container < prev    next >
Text File  |  1998-10-01  |  2KB  |  79 lines

  1. (*   
  2.  
  3. Class Container is intended to support persistent objects.  Once an object
  4. implements SEND: and BRING: correctly, we have to somehow link it to a file to
  5. make it persistent.  The Container  class represents one way that this can be
  6. done.  
  7.  
  8. Class Container is a subclass of File, and has an INIT: method in which you
  9. pass in the address of an object.  Then when you pass OPEN: and SAVE: messages
  10. to the Container, the Container sends BRING: and SEND: messages respectively,
  11. to the object.   
  12.  
  13. The fact that we only link one object to the Container doesn't mean that the
  14. number of objects in the Container is in any way limited, since the object can
  15. be a HandleList which can be a collection of an arbitrary number of objects
  16. from arbitary classes.  HandleList implements SEND: and BRING: properly (in a
  17. way that accounts for the objects in the list being of arbitrary classes). So,
  18. after you have sent OPEN: to a Container, all the objects in the container will
  19. have been read into memory.  And when you send SAVE:, they are all written out
  20. to the file.  If you don't want all your objects to be in memory at the same
  21. time, you can use several containers. 
  22.  
  23. This scheme is very flexible, and will probably be sufficient for most needs.
  24.  
  25. *)
  26.  
  27.  
  28. :class  CONTAINER  super{ file }
  29.  
  30.     dicaddr    ^obj
  31.     bool    setup?
  32.  
  33. :m INIT:  ( ^obj -- )
  34.     put: ^obj
  35.     false -> setup?
  36. ;m
  37.  
  38. :m OPEN:  { \ len #elements -- }
  39.     open: super  ?EXIT
  40.     ^base  get: ^obj  bring: []
  41.     close: super  drop
  42. ;m
  43.  
  44. :m SAVE:  { typ crtr addr1 len1 addr2 len2 \ pos -- }
  45.     get: setup?
  46.     NIF    addr1 len1 addr2 len2  stdPut: self  0EXIT
  47.         create: self  OK?
  48.         typ crtr set: self
  49.         true -> setup?
  50.     ELSE
  51.         open: super  OK?
  52.         0 setEOF: super  OK?
  53.     THEN
  54.     ^base  send: [ get: ^obj ]
  55.     close: super  drop
  56. ;m
  57.  
  58. ;class
  59.  
  60.  
  61. endload
  62.  
  63. container     CC
  64. handlelist    HH
  65.  
  66.  
  67. hh  init: cc
  68. " hoho" name: CC
  69.  
  70.  
  71. ' string newobj: hh  " hi there"  obj: hh  put: []
  72. ' string newobj: hh  " ho there"  obj: hh  put: []
  73. ' string newobj: hh  " what ho there"  obj: hh  put: []
  74.  
  75. 10 ' array newobj: hh   123 9  obj: hh  to: []
  76.                          456 8  obj: hh  to: []
  77.  
  78. endload
  79.